package servlet;

import model.Interaction;
import service.InteractionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
//插入收藏和想要的记录
@WebServlet(name = "RecordInteractionServlet", urlPatterns = "/record_interaction")
public class RecordInteractionServlet extends HttpServlet {
    private InteractionService interactionService = new InteractionService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("user_id"));
        int productId = Integer.parseInt(request.getParameter("product_id"));
        int type = Integer.parseInt(request.getParameter("type"));
        Date createTime = new Date();

        Interaction interaction = new Interaction();
        interaction.setUserId(userId);
        interaction.setProductId(productId);
        interaction.setType(type);
        interaction.setCreateTime(createTime);

        boolean isSuccess = interactionService.insertInteraction(interaction);
        if (isSuccess) {
            response.getWriter().write("互动记录成功");
        } else {
            response.getWriter().write("互动记录失败");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}